from core.config import settings
import requests

from core.logger import log
from db.models.chat_message import ChatMessageModel


async def get_reply_by_rag(message: str,from_user: str,to_user: str):
    """
    获取模型回复消息，传递发送的信息
    """
    session_id = ChatMessageModel.generate_session_id(send_id=from_user, receiver_id=to_user)
    data = {
        "text": message,
        "session_id": session_id
    }
    res = requests.post(settings.RAG_HOST+'/api/v1/query', json=data)
    # log.info(res)
    if res.status_code == 200:
        return res.json()["answer"]
    else:
        return None

async def add_texts(answer: str, question: str):
    try:
        URL = settings.RAG_HOST+ "/api/v1/add-texts"
        res = requests.post(URL, json={
            "answer": answer,
            "question": question
        })
        log.info(res.json())
        if res.status_code == 200:
            return True
        else:
            return False
    except Exception as e:
        log.info(f"rag_api中add_texts出现异常，{e}")
        return  False

async def add_qa(questions: list, answers: list,template_name:str):
    try:
        URL = settings.RAG_HOST+ "/api/v1/add-qa"
        res = requests.post(URL, json={
            "questions": questions,
            "answers": answers,
            "category":template_name
        })
        log.info(res.json())
        if res.status_code == 200:
            return True
        else:
            return False
    except Exception as e:
        log.info(f"rag_api中add_qa出现异常，{e}")
        return  False


